
import importlib


def import_lib(path_list, isspider=False):

    if isspider:
        result = {}
    else:
        result = []

    for path in path_list:
        # 截取字符串 rfind 从右边截取 +1: 去.
        class_name = path[path.rfind('.') + 1:]

        module_name = path[:path.rfind('.')]
        # 根据字符串 动态导包 实例化对象
        module = importlib.import_module(module_name)

        cls = getattr(module, class_name)

        if isspider:
            result[cls().name] = cls()
        else:
            result.append(cls())

if __name__ == '__main__':
    # 多爬虫
    SPIDERS = [
        'spiders.baidu.BaiduSpider'
    ]

    # 2. 多管道
    PIPELINES = [
        'pipelines.BaiduPipeline'
    ]

    import_lib_dynamic(SPIDERS, isspider=True)
    import_lib_dynamic(PIPELINES)

